package com.sojson.project.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.sojson.enums.ESalaryStatus;
import com.sojson.project.dao.SalaryDao;
import com.sojson.project.entity.po.Salary;
import com.sojson.project.entity.vo.SalaryVo;
import com.sojson.project.service.SalaryService;
import com.sojson.project.service.impl.thread.SalaryThreadImpl;
import com.sojson.util.ArithUtil;
import com.sojson.util.DateUtil;
import com.sojson.util.ExpUtil;
import com.sojson.util.MatcheUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.id.IdUtil;
import com.sojson.util.poi.ExcelUtil;

/**
 * 工资Service实现类
 * 
 * @author liu
 * @date 2021-05-23
 */
@Service
public class SalaryServiceImpl implements SalaryService {

    @Autowired
    private SalaryThreadImpl salaryThreadImpl;
    @Resource
    private SalaryDao salaryDaoImpl;

    /**
     * 单条插入
     * 
     * @param salary
     * @return
     */
    @Override
    public int insert(Salary salary) {
        int insert = salaryDaoImpl.insert(salary);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param salary
     * @return
     */
    @Override
    public int insertList(Salary salary) {
        int insertList = salaryDaoImpl.insertList(salary.getSalarys());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param salary
     * @return
     */
    @Override
    public int deleteByDto(Salary salary) {
        int deleteByDto = salaryDaoImpl.deleteByDto(salary);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @Override
    public int countAll() {
        int countAll = salaryDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @Override
    public List<Salary> listAll() {
        List<Salary> findAll = salaryDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param salary
     * @return
     */
    @Override
    public int countByDto(Salary salary) {
        int countByDto = salaryDaoImpl.countByDto(salary);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param salary
     * @return
     */
    @Override
    public List<Salary> listByDto(Salary salary) {
        List<Salary> findByDto = salaryDaoImpl.listByDto(salary);
        return findByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param salary
     * @return
     */
    @Override
    public List<SalaryVo> sendList(Salary salary) {
        List<SalaryVo> findByDto = salaryDaoImpl.sendList(salary);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = salaryDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        int deleteByIds = salaryDaoImpl.deleteByIds(ids);
        return deleteByIds;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateAllById(Salary salary) {
        int updateAllById = salaryDaoImpl.updateAllById(salary);
        return updateAllById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateAllByIdList(Salary salary) {
        int updateAllByIdList = salaryDaoImpl.updateAllByIdList(salary);
        return updateAllByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateDtoListAllById(Salary salary) {
        int updateDtoListAllById = salaryDaoImpl.updateDtoListAllById(salary.getSalarys());
        return updateDtoListAllById;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateById(Salary salary) {
        int updateById = salaryDaoImpl.updateById(salary);
        return updateById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateByIdList(Salary salary) {
        int updateByIdList = salaryDaoImpl.updateByIdList(salary);
        return updateByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateDtoListById(Salary salary) {
        int updateDtoListById = salaryDaoImpl.updateDtoListById(salary.getSalarys());
        return updateDtoListById;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @Override
    public Salary findById(String id) {
        Salary findById = salaryDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @Override
    public List<Salary> findByIds(Integer[] ids) {
        List<Salary> findByIds = salaryDaoImpl.findByIds(ids);
        return findByIds;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param salary
     * @return
     */
    @Override
    public List<Integer> findIdByDto(Salary salary) {
        List<Integer> findIdByDto = salaryDaoImpl.findIdByDto(salary);
        return findIdByDto;
    }

    /**
     * 通过userId删除(物理删除)
     * 
     * @param userId
     * @return
     */
    @Override
    public int deleteByUserId(Integer userId) {
        int deleteByUserId = salaryDaoImpl.deleteByUserId(userId);
        return deleteByUserId;
    }

    /**
     * 通过userId数组删除(物理删除)
     * 
     * @param userIds
     * @return
     */
    @Override
    public int deleteByUserIds(Integer[] userIds) {
        int deleteByUserIds = salaryDaoImpl.deleteByUserIds(userIds);
        return deleteByUserIds;
    }

    /**
     * 通过userId更新(全部更新)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateAllByUserId(Salary salary) {
        int updateAllByUserId = salaryDaoImpl.updateAllByUserId(salary);
        return updateAllByUserId;
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateAllByUserIdList(Salary salary) {
        int updateAllByUserIdList = salaryDaoImpl.updateAllByUserIdList(salary);
        return updateAllByUserIdList;
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容不相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateDtoListAllByUserId(Salary salary) {
        int updateDtoListAllByUserId = salaryDaoImpl.updateDtoListAllByUserId(salary.getSalarys());
        return updateDtoListAllByUserId;
    }

    /**
     * 通过userId更新(局部更新)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateByUserId(Salary salary) {
        int updateByUserId = salaryDaoImpl.updateByUserId(salary);
        return updateByUserId;
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容相同)
     * 
     * @param salary
     * @return
     */
    @Override
    public int updateByUserIdList(Salary salary) {
        int updateByUserIdList = salaryDaoImpl.updateByUserIdList(salary);
        return updateByUserIdList;
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容不相同)
     * 
     * @param salarys
     * @return
     */
    @Override
    public int updateDtoListByUserId(Salary salary) {
        int updateDtoListByUserId = salaryDaoImpl.updateDtoListByUserId(salary.getSalarys());
        return updateDtoListByUserId;
    }

    /**
     * 通过userId查询
     * 
     * @param userId
     * @return
     */
    @Override
    public Salary findByUserId(Integer userId) {
        Salary findByUserId = salaryDaoImpl.findByUserId(userId);
        return findByUserId;
    }

    /**
     * 通过userId数组查询
     * 
     * @param userIds
     * @return
     */
    @Override
    public List<Salary> findByUserIds(Integer[] userIds) {
        List<Salary> findByUserIds = salaryDaoImpl.findByUserIds(userIds);
        return findByUserIds;
    }

    /**
     * 通过对象查询userId列表
     * 
     * @param salary
     * @return
     */
    @Override
    public List<Integer> findUserIdByDto(Salary salary) {
        List<Integer> findUserIdByDto = salaryDaoImpl.findUserIdByDto(salary);
        return findUserIdByDto;
    }

    /**
     * 通过对象删除(逻辑删除)
     * 
     * @param salary
     * @return
     */
    @Override
    public int logicDeleteByDto(Salary salary) {
        int logicDeleteByDto = salaryDaoImpl.logicDeleteByDto(salary);
        return logicDeleteByDto;
    }

    /**
     * 统计所有未删除的数据
     * 
     * @return
     */
    @Override
    public int countAllByStatusNo() {
        int countAllByStatusNo = salaryDaoImpl.countAllByStatusNo();
        return countAllByStatusNo;
    }

    /**
     * 查询所有未删除的数据
     * 
     * @return
     */
    @Override
    public List<Salary> findAllByStatusNo() {
        List<Salary> findAllByStatusNo = salaryDaoImpl.findAllByStatusNo();
        return findAllByStatusNo;
    }

    /**
     * 通过id删除(逻辑删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int logicDeleteById(Integer id) {
        int logicDeleteById = salaryDaoImpl.logicDeleteById(id);
        return logicDeleteById;
    }

    /**
     * 通过id数组删除(逻辑删除)
     * 
     * @param ids
     * @return
     */
    @Override
    public int logicDeleteByIds(Integer[] ids) {
        int logicDeleteByIds = salaryDaoImpl.logicDeleteByIds(ids);
        return logicDeleteByIds;
    }

    /**
     * 通过userId删除(逻辑删除)
     * 
     * @param userId
     * @return
     */
    @Override
    public int logicDeleteByUserId(Integer userId) {
        int deleteByUserIdAndUpdate = salaryDaoImpl.deleteByUserId(userId);
        return deleteByUserIdAndUpdate;
    }

    /**
     * 通过userId数组删除(逻辑删除)
     * 
     * @param userIds
     * @return
     */
    @Override
    public int logicDeleteByUserIds(Integer[] userIds) {
        int logicDeleteByUserIds = salaryDaoImpl.logicDeleteByUserIds(userIds);
        return logicDeleteByUserIds;
    }

    /**
     * 清空数据(物理清空)
     */
    @Override
    public int clear() {
        return salaryDaoImpl.clear();
    }

    /**
     * 清空数据(逻辑清空)
     */
    @Override
    public int logicClear() {
        return salaryDaoImpl.logicClear();
    }

    /**
     * 导入工资
     * @param file 
     * @throws Exception 
     */
    @Override
    public int importSalary(MultipartFile file) throws Exception {
        ExcelUtil<Salary> util = new ExcelUtil<>(Salary.class);
        List<Salary> importExcel = util.importExcel(file.getInputStream());

        String uuid = IdUtil.uuid();
        // 检查导入的数据格式是否正确
        for (Salary salary : importExcel) {
            //
            if (StringUtil.isBlankObject(salary.getPostId())) {
                ExpUtil.throwEx("岗位ID不能为空！");
            }
            //
            Date issueDate = salary.getIssueDate();
            if (StringUtil.isBlankObject(issueDate)) {
                ExpUtil.throwEx("发放日期不能为空！");
            }
            String dateToString = DateUtil.dateToString(issueDate, DateUtil.YYYY_MM_DD);
            LocalDate stringToLocalDate = DateUtil.stringToLocalDate(dateToString, DateUtil.YYYY_MM_DD);
            Date localDateTimeToDate = DateUtil.localDateTimeToDate(stringToLocalDate);
            DateUtil.setHour(localDateTimeToDate);
            salary.setIssueDate(localDateTimeToDate);
            //
            String mobile = salary.getMobile();
            if (StringUtil.isBlankObject(mobile)) {
                ExpUtil.throwEx("手机号不能为空！");
            }
            if (!MatcheUtil.isMobile(mobile)) {
                ExpUtil.throwEx("手机号格式错误！");
            }
            //
            BigDecimal total = salary.getTotal();
            if (StringUtil.isBlankObject(total)) {
                ExpUtil.throwEx("金额不能为空！");
            }
            if (total.doubleValue() <= 0) {
                ExpUtil.throwEx("金额金额必须大于0！");
            }
            salary.setTotal(ArithUtil.allTwo(total));
            salary.insertInit(uuid);
        }

        // 添加
        salaryThreadImpl.importSalary(importExcel);
        return 1;
    }

    /**
     * 重新导入工资
     * 
     * @param id
     * @return
     */
    @Override
    public int reImportSalary(String id) {
        return salaryThreadImpl.reImportSalary(id);
    }

    /**
     * 确认工资
     * 
     * @param ids
     * @return
     */
    @Override
    public int affirm(Integer[] ids) {
        Salary salary = new Salary();
        salary.setStatus(ESalaryStatus.AFFIRM.getCode());
        salary.setIdArr(ids);
        return salaryDaoImpl.updateByIdArr(salary);
    }

    /**
     * 取消确认工资
     * 
     * @param ids
     * @return
     */
    @Override
    public int affirmCancle(Integer[] ids) {
        Salary salary = new Salary();
        salary.setStatus(ESalaryStatus.CREATE_YES.getCode());
        salary.setIdArr(ids);
        return salaryDaoImpl.updateByIdArr(salary);
    }

    /**
     * 发放工资
     * 
     * @return
     */
    @Override
    public void send() {
        salaryThreadImpl.send();
    }

    /**
     * 根据对象查询
     * 
     * @param salary
     * @return
     */
    @Override
    public List<Salary> list(Salary salary) {
        List<Salary> findByDto = salaryDaoImpl.list(salary);
        return findByDto;
    }

}